<?php 


class Users{
	
	public static $msgs = array();
	var $userid = '';
		
	function __construct()
	{
		$this->user_id = $_SESSION['admin_id'];
	}
	
		
	 public function addUser($data)
    {
	    global $db, $msgError;
		
		if(trim($data['username']) == '')
		{
			$this->msgs[] = 'User name can not be empty.';
		}
		else
		{
			if( $this->isUserExist($data['username'])){
				$this->msgs[] = 'User name already in use.';
			}
		}
				
		if(trim($data['name']) == '')
		{
			$this->msgs[] = 'User group can not be empty.';
		}
		if(trim($data['password']) == '')
		{
			$this->msgs[] = 'Password  can not be empty.';
		}
		if(trim($data['password']) != trim($data['con_password']))
		{
			$this->msgs[] = 'Password  and confirm password does not match.';
		}
		if((strlen(trim($data['password']))<6))
		{
			$this->msgs[] = 'Password  minimum length should be 6.';
		}
		if(($data['email']!='')&& !isValidEmail($data['email']))
		{
		   $this->msgs[] = 'Email address is invalid.';
		}
		if( $this->isEmailExist($data['email'])){
			$this->msgs[] = 'email address already in use.';
		}
		
		
		   
		     $module_ids_array =  $data['module_chbox'];
			 $record = array(
			                'group_id' => $data['group_id'],
			 			    'username' => $data['username'],
							'password' =>  md5($data['password']),
							'first_name' => $data['first_name'],
							'last_name' => $data['last_name'],
							'email' => $data['email']
							
							);
			 $id =  $db->insert("admin_users", $record); 
			
		

		
	 }
	function getUserById($user_id)
    {
	 	global $db;	
		$data = $db->first("SELECT * FROM trivia_users WHERE user_id = ".intval($user_id));
		return $data;
	 } 
	public function updateUser($data)
    {
 		global $db, $msgError;	

		clean_array($data);
		$user_id = intval($data['user_id']);
		if(trim($data['name']) == '')
		{
			$this->msgs[] = 'Name can not be empty.';
		}
		 
		
		if( empty($this->msgs) )
		{
			 $record = array(
			                'name' => $data['name'],
			 			    'phone_no' => $data['phone_no']
							);
 			
			$where = array( "user_id" =>$user_id );
			$db->update("trivia_users", $record, $where);
			redirect("admin/users.php?updated=1");		
			 
		}
		
		$msgError = '';
		foreach($this->msgs as $msg)
		{
			$msgError .= '<p>'.$msg.'</p>';
		}
	 			
	} 
	function getUser($data)
	{
		//print_r($data);
		global $db, $pager, $config;
		$pager = new Paginator();
		
		$where = '';
		if( isset($data['username']) && $data['username'] != '')
		{
			$where = " where name like '%" . $db->escape($data['username']) . "%'";
		}
		
		if( isset($data['new_page_limit']) && intval($data['new_page_limit']) > 0 ){
			$config['page_limit'] = $data['new_page_limit'];
		}
		$page_limit = $config['page_limit']; 
		if(isset($_GET['pg'])&& $_GET['pg'] > 0 ){ 
			$limit_lenght  = (($_GET['pg']-1)*$page_limit) ; 
			
			$limit = "limit $limit_lenght, $page_limit ";
		}else{
			$limit = "limit 0 , $page_limit ";
			if($data['new_page_limit'] == 'all') 
				$limit = '';
		}
		
	   $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM trivia_users  $where $limit";      

      $result = $db->fetch_all($sql);
 		$counter = $db->queryCount();
		$pager->items_total = $counter[0];
		$pager->default_ipp = ($data['new_page_limit'] == 'all') ? $pager->items_total : $config['page_limit'];
		$pager->paginate($path);
		return $result;
		
	}
	
	/*function deleteUser($id)
	{
		global $db, $msgError;
		if( empty($this->msgs) )
		{
			$where = array( 'id' => $id );
			$where_per = array( 'group_id' => $id );
			if( $db->delete("admin_users", $where) )
			{ 
			   $db->delete("module_permission", $where_per);
			   redirect("admin/users.php?deleted=1");	
			}
			else
			{
				$this->msgs[] = 'Some error occurs. Group can not be deleted.';
			}
		}
	    $msgError = '';
		foreach($this->msgs as $msg)
		{
			$msgError .= '<p>'.$msg.'</p>';
		}
		
	}*/
	//following function user for User Group
	
  
}


?>